package com.tour.modules.web.controller.api;

import com.tour.modules.web.entity.TUserEntity;
import com.tour.modules.web.entity.TWorkOrderContentEntity;
import com.tour.modules.web.entity.TWorkOrderEntity;
import com.tour.modules.web.service.TUserService;
import com.tour.modules.web.service.TWorkOrderContentService;
import com.tour.modules.web.service.TWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
public class TWorkApi {
    @Autowired
    private TUserService tUserService;
    @Autowired
    private TWorkOrderService tWorkOrderService;
    @Autowired
    private TWorkOrderContentService tWorkOrderContentService;

    //通过id获取信息
    @RequestMapping("/api/twork/getinfo")
    @ResponseBody
    public List<TWorkOrderContentEntity> getinfo(HttpServletRequest request){
        TWorkOrderEntity tWorkOrder = tWorkOrderService.queryObjectByUserId(Long.parseLong(request.getParameter("id")));
        List<TWorkOrderContentEntity> tWorkOrderContentEntity = tWorkOrderContentService.queryListByTWorkOrderId(tWorkOrder.getId());
        return tWorkOrderContentEntity;
    }
    @RequestMapping("api/work/submit")
    public String submit(HttpServletRequest request){
        Long user_id=Long.parseLong(request.getParameter("user_id"));
        TWorkOrderEntity tWorkOrder1 = tWorkOrderService.queryObjectByUserId(user_id);
        if(tWorkOrder1==null){
            TUserEntity user=tUserService.queryObject(user_id);
            TWorkOrderEntity tWorkOrder=new TWorkOrderEntity();
            tWorkOrder.setUserId(user_id);
            tWorkOrder.setMobile(user.getMobile());
            tWorkOrder.setEmail(user.getEmail());
            tWorkOrder.setCreateTime(new Date());
            tWorkOrder.setCUpdateTime(new Date());
            tWorkOrderService.save(tWorkOrder);
            TWorkOrderContentEntity tWorkOrderContentEntity=new TWorkOrderContentEntity();
            tWorkOrderContentEntity.setCexWorkOrderId(tWorkOrder.getId());
            tWorkOrderContentEntity.setStatue(0);
            tWorkOrderContentEntity.setContent(request.getParameter("content"));
            tWorkOrderContentEntity.setUserId(user_id);
            tWorkOrderContentEntity.setCreateTime(new Date());
            tWorkOrderContentService.save(tWorkOrderContentEntity);

        }else{

            TWorkOrderContentEntity tWorkOrderContentEntity=new TWorkOrderContentEntity();
            tWorkOrderContentEntity.setCexWorkOrderId(tWorkOrder1.getId());
            tWorkOrderContentEntity.setStatue(0);
            tWorkOrderContentEntity.setContent(request.getParameter("content"));
            tWorkOrderContentEntity.setUserId(user_id);
            tWorkOrderContentEntity.setCreateTime(new Date());
            tWorkOrderContentService.save(tWorkOrderContentEntity);
        }
        return  "redirect:/front/twork.jsp";
    }
}
